<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Count extends Base_Controller{
	
	// 学校id
	private $token;
	private $school_id;
	
	// 追加查询条件
	private $_where_in = [];

	public function __construct(){
		parent::__construct();
		$this->valid_user();
		$this->load->model('signup_record_model');
	}
	
	private function valid_user(){
		$wx_openid = $this->input->cookie('wx_openid');
		// $wx_openid = 'oCbvcvw3Qol7kSVN2QdjdZpN6sy4';
		if(!$wx_openid){
			$this->load->library('Wechat/wechatjs');
			$wx_openid = $this->wechatjs->get_openid();
			$this->input->set_cookie('wx_openid', $wx_openid, 3600*24*30);
		}
		$user_openids = [
			'oCbvcvw3Qol7kSVN2QdjdZpN6sy4' => true,
			'oCbvcvyoirCSEbSUST9t8X8sYMhc' => true,
			'oCbvcv5X0827p3aVr6T4qj4MAkOg' => true,
			'oCbvcv47m9HK-1O5CLSVnOe8vSdk' => true,
			'oCbvcv258oLi33LVT8vMLDX5zwMk' => [4],
			'oCbvcv3z6cO5dne2BJnaBTeUTCdY' => [3, 6],
			'oCbvcv1hya2Cc5dyks-5Zx-T-1lE' => true
		];
		$school_id = $user_openids[$wx_openid];
		if(!$school_id){
			$this->show_message('没有访问权限，请联系管理员');
		}
		if($school_id !== true){
			$this->school_id = $school_id;
			$this->_where_in = [
				'school_id' => $school_id
			];
		}
	}
	
	public function index(){
		$this->load->helper('array');
		$this->load->model('school_model');
		
		if($this->school_id){
			$school_where = [
				'where_in' => [
					'id' => $this->school_id
				]
			];
		}
		
		$school_list = $this->school_model->get_result($school_where);
		
		// 7天数据
		$maxlen = 6;
		for($i = $maxlen; $i >= 0; $i--){
			$date_list[] = date("Y-m-d", strtotime("-$i day"));
		}
		
		$time_start = strtotime($date_list[0]);
		$time_end = strtotime(date('Y-m-d 23:59:59'));
		
		// 查询条件
		$where = [
			'inputtime >=' => $time_start,
			'inputtime <=' => $time_end
		];
		
		$records = $this->signup_record_model->get_result([
			'select' => ['FROM_UNIXTIME(inputtime,"%Y-%m-%d") as day', 'school_id', 'school', 'count(id) as total'],
			'where' => $where,
			'where_in' => $this->_where_in,
			'group_by' => ['day', 'school_id'],
			'order_by' => 'day desc'
		]);
		$lastest_list = array_column2($records, 'day');
		
		// 累计
		$total_list = $this->signup_record_model->get_result([
			'select' => ['school_id', 'school', 'count(id) as total'],
			'where_in' => $this->_where_in,
			'group_by' => ['school_id'],
			'order_by' => 'school_id asc'
		]);
		$total_list = array_column($total_list, null, 'school_id');
		
		// 今日
		$totay_arr = array_column($lastest_list[date('Y-m-d')], null, 'school_id');
		
		// 曲线数据配置
		$chart_data = [];
		foreach($date_list as $date){
			$chart_data['xAxis_data'][] = date("m-d", strtotime($date));
		}
		
		// 人数
		$total_num = 0;
		$today_num = 0;
		
		// 当天数据
		$sch_dayarr = array_column2($records, 'school_id');
		
		
		$count_list = [];
		foreach($school_list as $item){
			$sch_id = $item['id'];
			$row = $total_list[$sch_id];
			
			$row['today_num'] = (int)$totay_arr[$sch_id]['total'];
			
			$today_num += $row['today_num'];
			$total_num += $row['total'];
			
			// 其他数据
			$dayarr = $sch_dayarr[$sch_id];
			$dayarr = array_column($dayarr, null, 'day');
			$series_data = [];
			foreach($date_list as $date){
				$series_data[] = (int)$dayarr[$date]['total'];
			}
			$chart_data['series'][] = [
				'name' => $row['school'],
				'data' => $series_data
			];
			
			$count_list[] = $row;
		}
		
		$assign = [
			'token' => $this->token,
			'chart_data' => $chart_data,
			'count_num' => [
				'today' => $today_num,
				'total' => $total_num
			],
			'count_list' => $count_list
		];
		
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('count_index.htm');
	}
	
	public function list(){
		$school_id = $this->input->get('school_id');
		if($this->school_id && !in_array($school_id, $this->school_id)){
			$this->show_message('没有访问权限，请联系管理员');
		}
		$list = $this->signup_record_model->get_result([
			'select' => ['id', 'school', 'grade', 'campus', 'student_name', 'inputtime'],
			'where' => [
				'school_id' => $school_id
			]
		]);
		$assign = [
			'token' => $this->token,
			'list' => $list
		];
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('count_list.htm');
	}
	
}